﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModbusTestTool.Error_Logging;

namespace ModbusTestTool.UI
{
    public partial class ErrorPanel : UserControl
    {
        public ErrorPanel()
        {
            InitializeComponent();
            ErrorLog.Instance.errorHandler = HandleError;

            
        }
        
        List<ErrorLog.ErrorLogMessage> list = new List<ErrorLog.ErrorLogMessage>();

        public void HandleError()
        {

            if (dataGridView1.InvokeRequired)
            {
                dataGridView1.Invoke(new MethodInvoker(HandleErrorHandler));
            }
            else
            {
                HandleErrorHandler();
            }
        }

        private void HandleErrorHandler()
        {
            ErrorLog.ErrorLogMessage temp;
            while ((temp = ErrorLog.Instance.getMessage()) != null)
            {
                list.Add(new ErrorLog.ErrorLogMessage() { time = temp.time, message = temp.message });
            }

            if (dataGridView1.DataSource == null)
            {
                dataGridView1.DataSource = list;
                dataGridView1.AutoGenerateColumns = true;
            }
            dataGridView1.DataSource = typeof(List<>);
            dataGridView1.DataSource = list;
            dataGridView1.Update();
            dataGridView1.Refresh();
        }

    }
}
